package com.bawei.park2302.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.utils.utils.entity.NoticeBeanItem
import com.bawei.lib_base.utils.utils.mvi.BaseFragment
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.park2302.adapter.NoticeAdapter
import com.bawei.park2302.databinding.FragmentHomeBinding
import com.bawei.park2302.notice.NoticeIntent
import com.bawei.park2302.notice.NoticeViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding,NoticeIntent,NoticeViewModel>() {
    override val binding: FragmentHomeBinding by lazy {
        FragmentHomeBinding.inflate(layoutInflater)
    }
    override val vm: NoticeViewModel by viewModels<NoticeViewModel>()

    val noticeAdapter by lazy { NoticeAdapter() }

    override fun initView() {
        binding.rvNotice.apply {11
            adapter = noticeAdapter
            layoutManager = LinearLayoutManager(context)
        }

        binding.llHouse.setOnClickListener {
            ARouter.getInstance().build("/app/HouseActivity").navigation()
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        noticeAdapter.addData(it.data as Collection<NoticeBeanItem>)
                        noticeAdapter.notifyDataSetChanged()
                    }

                    else -> {}
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(NoticeIntent.notice(0,10))
    }
}